﻿using LitJson;

namespace NetFrame{
    /// <summary>
    /// <para>ConnectedError 连接服务器失败</para>
    /// <para>Successed 处理数据成功</para>
    /// <para>Failed 和数据库中数据不同，如:帐号密码错误</para>
    /// <para>NoneData 数据库中无此数据</para>
    /// <para>DBError 操作数据库失败,如:操作数据库时报错</para>
    /// <para>LogicNonentity 访问的方法不存在</para>
    /// </summary>
    public enum HttpReturnStatus {
        ConnectedError,
        Successed,
        Failed,
        NoneData,
        DBError,
        LogicNonentity,
        DataError
    }

    public class HttpResponse
    {
        private string data;
        private HttpReturnStatus status;
        private int httpProtocol;
        private string errStr;
        public HttpResponse (string respText)
        {
            JsonData jd = JsonMapper.ToObject(respText);
            try
            {
                data = jd["data"].ToJson();
                status = (HttpReturnStatus)int.Parse(jd["status"].ToJson());
                httpProtocol = int.Parse(jd["httpProtocol"].ToJson());
                switch (status) {
                    case HttpReturnStatus.ConnectedError:
                        errStr = "链接失败！";
                        break;
                    case HttpReturnStatus.Failed:
                        errStr = "链接失败！";
                        break;
                    case HttpReturnStatus.NoneData:
                        errStr = "无数据！";
                        break;
                    case HttpReturnStatus.DBError:
                        errStr = "连接数据库时发生错误！";
                        break;
                    case HttpReturnStatus.LogicNonentity:
                        errStr = "服务器逻辑不存在！";
                        break;
                }
            }
            catch {
                status = HttpReturnStatus.DataError;
                errStr = "数据格式错误！";
            }
        }

        public string GetData() {
            return data;
        }

        public int GetHttpProtocol() {
            return httpProtocol;
        }

        public HttpReturnStatus GetStatus() {
            return status;
        }

        public string GetErrorString()
        {
            return errStr;
        }
    }
}
